package com.example.scaletest;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mylibrary.scaleweihgt.AndroidUtil;

import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.StrUtil;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class ScaleWindow {
    private Context mContext;
    private PopupWindow mPopupWindow;
    private OnScaleClickListener mListener;
    int internalCount;
    TextView tv_inner;
    private float weight;
    private Disposable mDisposable;

    public interface OnScaleClickListener {

        void onClickOne();
        void onClickTwo(float f);
    }

    public ScaleWindow(Context context) {
        mContext = context;
        initPopupWindow();
    }

    public int getInternalCount() {
        return internalCount;
    }

    public void setInternalCount(int internalCount) {
        this.internalCount = internalCount;
        tv_inner.setText(internalCount+"");
    }

    private void initPopupWindow() {
        View popupView = LayoutInflater.from(mContext).inflate(R.layout.popup_scale, null);
        RelativeLayout ll_chose_set = popupView.findViewById(R.id.ll_chose_set);
        ll_chose_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mPopupWindow.dismiss();
            }
        });

        LinearLayout tep_one = popupView.findViewById(R.id.tep_one);
        LinearLayout tep_two = popupView.findViewById(R.id.tep_two);
        LinearLayout tep_three = popupView.findViewById(R.id.tep_three);
        LinearLayout tep_four = popupView.findViewById(R.id.tep_four);

        TextView tv_one_content = popupView.findViewById(R.id.tv_one_content);
        TextView tv_two_content = popupView.findViewById(R.id.tv_two_content);
        TextView tv_three_content = popupView.findViewById(R.id.tv_three_content);
        TextView tv_four_content = popupView.findViewById(R.id.tv_four_content);
        Button btn_cancel = popupView.findViewById(R.id.btn_cancel);
        TextView tv_inner = popupView.findViewById(R.id.tv_inner);

        TextView finalTv_inner = tv_inner;
        mDisposable = Observable.interval(0, 60, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(mTimer -> {
                    finalTv_inner.setText(SpUtil.get(Config.INNER_CODE,""));
                });


        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mDisposable != null && mDisposable.isDisposed()) {
                    mDisposable.dispose();
                }
                dismiss();
            }
        });

        popupView.findViewById(R.id.bt_one).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onClickOne();
                tep_one.setVisibility(View.GONE);
                tep_two.setVisibility(View.VISIBLE);
            }
        });

        EditText et_input = popupView.findViewById(R.id.et_input);
        popupView.findViewById(R.id.bt_two).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    String wei = et_input.getText().toString();
                    if (StrUtil.isEmpty(wei)){
                        Toast.makeText(mContext, R.string.enter_the_weight, Toast.LENGTH_SHORT).show();
                        return;
                    }
                    weight = Float.parseFloat(wei);
                } catch (Exception e) {
                    Toast.makeText(mContext, "请输入正确的标重重量", Toast.LENGTH_SHORT).show();
                   return;
                }
                tep_two.setVisibility(View.GONE);
                tep_three.setVisibility(View.VISIBLE);
                tv_three_content.setText(mContext.getString(R.string.step_three_one)+weight+mContext.getString(R.string.step_three_two));
            }
        });
        popupView.findViewById(R.id.bt_three).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onClickTwo(weight);
                tep_three.setVisibility(View.GONE);
                tep_four.setVisibility(View.VISIBLE);
            }
        });

        popupView.findViewById(R.id.bt_four).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDisposable != null && mDisposable.isDisposed()) {
                    mDisposable.dispose();
                }
               dismiss();

            }
        });


        mPopupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow的背景为半透明
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x80000000));

        mPopupWindow.setOutsideTouchable(true);
    }

    public void SetOnScaleClickListener(OnScaleClickListener listener) {
        mListener = listener;
    }
    public void dismiss() {
        if (mDisposable != null && mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
        mPopupWindow.dismiss();
    }
    public void show(View anchorView) {


        if (Build.VERSION.SDK_INT < 24) {
            mPopupWindow.showAsDropDown(anchorView);
        } else {
            if (mContext instanceof Activity) {
                if (null != mPopupWindow) {
                    int screenHeight;
                    // 获取屏幕可用高度：真实高度-虚拟按键的高度
                    screenHeight = AndroidUtil.getContentHeight((Activity) mContext);
                    int[] location = new int[2];
                    // 获取控件在屏幕的位置
                    anchorView.getLocationOnScreen(location);
                    // pop最大高度
                    int maxHeight = screenHeight - location[1] - anchorView.getHeight();
                    // pop 有具体的高度值，但是小于anchor下边缘与屏幕底部的距离， 正常显示
                    if (mPopupWindow.getHeight() > 0 && mPopupWindow.getHeight() < maxHeight) {
                        mPopupWindow.showAsDropDown(anchorView);
                    } else {
                        //设置为最大可用高度
                        mPopupWindow.setHeight(maxHeight);
                        mPopupWindow.showAsDropDown(anchorView,0,0);
                    }
                }


            }
        }
    }

}
